/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.myontology.controller.impl.MyOntologyController;
import org.myontology.users.Login;

/**
 * @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 *
 */
public class ControllerServlet extends HttpServlet {

	
	public static final ValueHandler<PageProvider> PAGE = new ValueHandler<PageProvider>(AttributeLevel.REQUEST,ControllerServlet.class);
	
	protected AbstractController controller;
	
	Logger logger = Logger.getLogger(getClass());
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		String controllerImpl = config.getInitParameter("ControllerImplementation");
		controller = new MyOntologyController();
		if (controllerImpl!=null) {
			try {
				Class clazz = Class.forName(controllerImpl);
				AbstractController con = (AbstractController)clazz.newInstance();
				controller = con;
			}
			catch (Exception e) {
				logger.error(controller+" was given as implementation of AbstractController but had an error when initializing",e);
			}
		}
			
		
	}
	
	protected void doWork(HttpServletRequest req, HttpServletResponse resp) {
		WebAttributeHolder attributes = new WebAttributeHolder(req);
		String actionPath = req.getServletPath();
		String fullPath = actionPath+"?"+req.getQueryString();
		actionPath=actionPath.substring(1);
		actionPath=actionPath.substring(0,actionPath.lastIndexOf('.'));
		attributes.setRequestAttribute("action", actionPath);
		PageProvider prov = controller.execute(attributes);
		Login.LAST_PATH.setValue(new WebAttributeHolder(req), fullPath);
		String jsp = "/index.jsp";
		if (prov!=null) {
			PAGE.setValue(attributes, prov);
			if (prov.getRedirect(attributes)!=null) {
				try {
					String url = prov.getRedirect(attributes);
					if (url.startsWith("http"))
						resp.sendRedirect(url);
					else
						resp.sendRedirect( req.getContextPath() + prov.getRedirect(attributes));
					return;
				} catch (Exception e) {
					logger.error("problems redirecting to: "+prov.getRedirect(attributes)+" full path: "+req.getContextPath() + prov.getRedirect(attributes),e);
				}
			}
			if (prov.getForward()!=null)
				jsp=prov.getForward();
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(jsp);
		try {
			dispatcher.forward(req, resp);
		} catch (Exception e) {
			logger.error("problems forwarding to: "+jsp,e);
		} 
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doWork(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doWork(req, resp);
	}
	
}
